//EasyJS.js by Lucas Vendramini - 2014

function divL(id, altura, largura, cor, position , x, y, radius,inner, inner_color, pleft, ptop, mouse){
   if(id){  
      this.id = id; //id do elemento
   }
   if(altura){//altura do elemento
      this.altura = altura;
   }
   if(largura){//largura do elemento
      this.largura = largura;
   }
   if(cor){
      this.cor = cor;//cor do elemento
   }
   if(position){
      this.position = position;//posicionamento, relativo ou absoluto
   }
   if(x){
      this.x = x;//posicao no eixo X
   }
   if(y){
      this.y = y;//posicao no eixo Y
   }   
   if(radius){
      this.radius = radius;//raio da borda
   }
   if(inner){
      this.inner = inner;//children do elemento
   }
   if(inner_color){
      this.inner_color = inner_color;//cor do texto dentro do elemento
   }
   if(pleft){//padding left
      this.pleft = pleft;
      this.largura = largura - pleft;
   }  
   if(ptop){//padding top
      this.ptop = ptop;
      this.altura = altura - ptop;
   }
   if(mouse == true || mouse == false){//movimento do mouse
   
      this.mouse = mouse;
   }
}

divL.prototype.altura = 500;
divL.prototype.largura = 500;
divL.prototype.cor = "#aaa";
divL.prototype.position = "relative";
divL.prototype.x = 0;
divL.prototype.y = 0;
divL.prototype.radius = 0; 
divL.prototype.inner = "";
divL.prototype.inner_color = "#000";
divL.prototype.pleft = 5;
divL.prototype.ptop = 5;
divL.prototype.mouse = false;

divL.prototype.cria = function(parent){

   var elem = document.createElement("DIV");
   elem.id = this.id;   

   elem.style.height = this.altura+"px";
   elem.style.width = this.largura+"px";
   elem.style.backgroundColor = this.cor;
   
   elem.style.position = this.position;
   
   elem.style.left = this.x+"px";
   elem.style.top = this.y+"px";
   elem.style.borderRadius = this.radius+"px";



   var inside = false;


    elem.addEventListener("mouseenter", function(){
      inside = true;
   });
   
   elem.addEventListener("mouseout", function(){
      inside = false;
   });
   
   window.addEventListener("keypress", function(e){
   
   
      var code;
      if (!e){
         e = desenho.event;
      // No IE, preciso capturar o evento
      }
      if (e.keyCode){
      // IE e Chrome
         code = e.keyCode;
      }
      else if (e.which)
      { // Firefox
         
         code = e.which;
      }  
      
      if(code == 8){//backspace
      
         elem.textContent.length -= 1;
      }
           
      var char = String.fromCharCode(code);
      
      if(inside){
         console.log(true);
         elem.textContent += char;
      }
         
   });

   elem.textContent = this.inner;
   elem.style.color = this.inner_color;
   
   
   elem.style.paddingLeft = this.pleft+"px";
   elem.style.paddingTop = this.ptop+"px";

   var click = false;
   
   elem.addEventListener("mousedown", function(e){
       click = true;
   });
   window.addEventListener("mouseup", function(e){
       click = false;
   });
   window.addEventListener("mousewheel", function(e){
       this.radius += 0.1;
   });

   elem.addEventListener("mousemove",function(e){
           
      if (click){
         var x = e.clientX - 100;
         var y = e.clientY- 100;
                 
         elem.style.left = x+"px";
         elem.style.top = y+"px";
      }
   
   }); 

   if(parent){
      document.getElementById(parent).appendChild(elem);
   }
   else{
      document.body.appendChild(elem);
   }
} 

divL.prototype.apaga = function(){

   document.getElementById(this.id).remove();
}

//----------------------------------------------------------------------------

var teste = new divL("nada", 300, 400, "#ff0");

teste.mouse = true;
teste.radius = 50;
teste.cria();
